package screen;

import gameTypes.Drawable;
import globals.Fonts;
import globals.Global;

import java.awt.Color;
import java.awt.Graphics;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import screen.item.EditField;
import screen.item.ScreenItem;
import screen.item.TextButton;

/**
 * @author Ben Elliott
 */
public class MenuScreen implements Drawable {
	List<ScreenItem> buttons;
	float x = 10;
	int y = 10;
	
	EditField serverAddr, userName;

	public MenuScreen() {
		buttons = new ArrayList<ScreenItem>();
	}
	
	public void Load() {
		/* EditBox */
		int width = 160;
		String local;
		try	{
			local = InetAddress.getLocalHost().getHostAddress() + ":" + Global.Instance().port;
		}
		catch (UnknownHostException ex)	{
			local = "Network Error";
		}
		serverAddr = new EditField(local, false, Global.Instance().winWidth/2 - width/2, 300, 160, 32, Fonts.menu) {
			@Override
			public void mouseClick() {
				if (mouseOver)
					this.selected = true;
				else
					this.selected = false;
			}
		};
		buttons.add(serverAddr);
		
		Global.Instance().username = System.getProperty("user.name");
		userName = new EditField(Global.Instance().username, true, Global.Instance().winWidth/2 - width/2, 350, 160, 32, Fonts.menu) {
			@Override
			public void mouseClick() {
				if (mouseOver)
					this.selected = true;
				else
					this.selected = false;
			}
		};
		buttons.add(userName);
		
		/* Buttons */
		width = Fonts.menuMetrics.stringWidth("Log In");
		buttons.add(new TextButton("Log In", Global.Instance().winWidth/2 - width/2, 400, new Color(0.9f,0.9f,0.9f,1.0f), Fonts.menu, Fonts.menuMetrics) {
			@Override
			public void mouseClick() {
				if (mouseOver) {
					Global.Instance().username = userName.getText();
					if (Global.Instance().networkManager.connect(serverAddr.getText()))
						Global.Instance().currentScreen = ScreenType.Game;
				}
			}
			@Override
			public void mouseOver() {
				mouseOverColor = new Color(0.8f,0.0f,0.0f,1.0f);
			}});
		
		width = Fonts.menuMetrics.stringWidth("Exit");
		buttons.add(new TextButton("Exit", Global.Instance().winWidth/2 - width/2, 450, new Color(0.9f,0.9f,0.9f,1.0f), Fonts.menu, Fonts.menuMetrics) {
			@Override
			public void mouseClick() {
				if (mouseOver)
					Global.Instance().gameRunning = false;
			}
			@Override
			public void mouseOver() {
				mouseOverColor = new Color(0.8f,0.0f,0.0f,1.0f);
			}});
	}
	
	@Override
	public void Update(float gameTime) {
		// Update All buttons
		for (ScreenItem s : buttons) {
			s.Update(gameTime);
		}
	}

	@Override
	public void Draw(Graphics graphics) {
		// Backdrop
		graphics.setColor(new Color(0.3f,0.3f,0.3f,1.0f));
		graphics.fillRect(0, 0, Global.Instance().winWidth, Global.Instance().winHeight);
		
		// Title
		int width = Fonts.titleMetrics.stringWidth("Aoura 2")/2;
		graphics.setFont(Fonts.title);
		graphics.setColor(Color.WHITE);
		graphics.drawString(
				"Aoura 2",
				Global.Instance().winWidth/2 - width,
				250);
		
		// Draw all buttons
		for (ScreenItem s : buttons) {
			s.Draw(graphics);
		}
	}
}
